'use strict';

app.controller('user_detail_Ctrl', ['$scope','$http','loginService','$routeParams', function($scope,$http,loginService,$routeParams){
	 $scope.init = function() {
		     loginService.islogged();
             $scope.link = loginService.getLink();			 
	    	 $scope.getUserById($routeParams.id,1);
	    };
	
	  //=====================paging groups of user======================
    $scope.entryLimit_group = 5
    $scope.offset_group = 0;
    $scope.disablePrev_group = true;
    $scope.disableNext_group = false;
    $scope.page_now_group = 1;
    //================================================================
	
	 $scope.getUserById = function(id, type){
				var name = 'not_name';
			        $http.get($scope.link+'users/'+id).success(function(data){
			        	console.log('owner is loaded');
			            if(type==1){
			        	  $scope.owner = data;
			            }
			        	
			         
			        	 $scope.getGroupsByUserId(data.data.id);
			        }).success(function(data, status, config) {
			        	 $scope.name = data.data.first_name + ' ' + data.data.last_name;
			        });
			    
			    
			 }
	 

	 $scope.getGroupsByUserId = function(id){
	        $http.get($scope.link+'groups/'+id).success(function(data){
	        	console.log('groups is loaded');
	        	$scope.groups = data;
				//$scope.getListGroupsId(data);
				var lst =  new Array();
				lst= $scope.getListGroupsId(data);
				
				$scope.getGroupsByListId(lst);
	        	//$scope.getUsersByGroupId(id);
	        	// $scope.jsonToArray_groups(data);
	        	 
				// alert(JSON.stringify($scope.b) );
	        }).error(function(data, status, config) {
	        	console.log('can not load list of groups with user id : ' + id);
	 });
	 }
	
	
	 $scope.getGroupsByListId = function(ids){
	        $http.get($scope.link+'groups_by_ids/'+ids).success(function(data){ 
	        	console.log('list groups is loaded');
	        	
			$scope.b = new Array();
			var i = 0;
			angular.forEach(data.data,function(){
				$scope.b[i]=new Array(2);
	    		$scope.b[i][0] =  data.data[i].name;
				$scope.b[i][1] =  data.data[i].id;
	            i++;
	        })
	        $scope.nbr_rows_groups = i ;
			
			$scope.b_ini= angular.copy($scope.b);
				
			pageResult_group($scope);	
	        }).error(function(data, status, config) {
	        	console.log('can not load list of groups with list Id groups : ' + id);
	        });
	 }
	
	 $scope.jsonToArray_groups = function (data) {
			$scope.b = new Array();
			var i = 0;
			angular.forEach(data.data.groups_list,function(){
				$scope.b[i]=new Array(2);
	    		$scope.b[i][0] =  data.data.groups_list[i].name;
				$scope.b[i][1] =  data.data.groups_list[i].id;
	            i++;
	        })
	        $scope.nbr_rows_groups = i ;
			
			$scope.b_ini= angular.copy($scope.b);
		}
	
	
	 $scope.getListGroupsId = function (data){
		 var lst =  new Array();
		 var i = 0;
		 angular.forEach(data.data.groups_list,function(){
			lst[i] = data.data.groups_list[i].id;
			i++;
		 })
		 return lst;
	 }
	 
	
	 

	 	$scope.$watch('b', function() {   
			if( ($scope.page_now_group==1)  &&  ($scope.nbr_rows_groups <= $scope.entryLimit_group) )
			 {
			 $scope.disableNext_group = true;
			 }
			
		}); 
	 
		// switch to next page
	$scope.nextPage_group = function() { 
       nextPage_common_group($scope);
	}

	// switch to previous page
	$scope.previousPage_group = function() {
      previousPage_common_group($scope);
	}
	
	$scope.$watch('b', function() {   
			if( ($scope.page_now_group==1)  &&  ($scope.nbr_rows_groups <= $scope.entryLimit_group) )
			 {
			 $scope.disableNext_group = true;
			 }
			
		}); 
		 $scope.lock = function(userid,id){
		  if(!loginService.verify_admin()){
			 alert('You are not allow to block or unnlock this user');
			 id=3;
		  }
		
		  if(id==0){
			
			
				  $http.get($scope.link+'user_bl/'+userid).success(function(data){
			        	console.log('user is locked');
			        	//$scope.lock = data;
			         	//$scope.list_user($scope.offset,$scope.page_now);
			         }).error(function(data, status, config) {
			        	console.log('can not lock user with id : ' + userid);
			       });
			 
		  }
		  else if(id==1){
			  $http.get($scope.link+'user_ubl/'+userid).success(function(data){
		        	console.log('user is unlocked');
		        	$scope.unlock = data;
		        	//alert('unlock :   '  +  JSON.stringify(data));
		        	//$scope.getGroupsByUserId(data.data.id);
		        	 $scope.list_user($scope.offset,$scope.page_now);
		         }).error(function(data, status, config) {
		        	console.log('can not un lock user with id : ' + userid);
		       });  
		  }
		  
		  $scope.getUserById(userid,1);
		  //$scope.go(userid,0,1);  
		 
	 }
	
}])